<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="831px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" :rules="formRules" :inline="true">
                <el-form-item label="中文名" prop="chineseName">
                    <el-input v-model="formData.chineseName" placeholder="请输入中文名" />
                </el-form-item>
                <el-form-item label="英文名" prop="englishName">
                    <el-input v-model="formData.englishName" placeholder="请输入英文名" />
                </el-form-item>
                <el-form-item label="工号" prop="jobNumber">
                    <el-input v-model="formData.jobNumber" placeholder="请输入工号" />
                </el-form-item>
                <el-form-item label="名称/姓(中文)" prop="surnameChinese">
                    <el-input v-model="formData.surnameChinese" placeholder="请输入名称/姓(中文)" />
                </el-form-item>
                <el-form-item label="名(中文)" prop="nameChinese">
                    <el-input v-model="formData.nameChinese" placeholder="请输入名(中文)" />
                </el-form-item>
                <el-form-item label="名称/姓(英文)" prop="surnameEnglish">
                    <el-input v-model="formData.surnameEnglish" placeholder="请输入名称/姓(英文)" />
                </el-form-item>
                <el-form-item label="名(英文)" prop="nameEnglish">
                    <el-input v-model="formData.nameEnglish" placeholder="请输入名(英文)" />
                </el-form-item>
                <el-form-item label="国籍(地区)" prop="nationality">
                    <el-input v-model="formData.nationality" placeholder="请输入国籍(地区)" />
                </el-form-item>

                <el-form-item label="国家(地区" prop="nation">
                    <el-input v-model="formData.nation" placeholder="请输入国家(地区)" />
                </el-form-item>
                <el-form-item label="省份" prop="province">
                    <el-input v-model="formData.province" placeholder="请输入省份id" />
                </el-form-item>
                <el-form-item label="城市(中文)" prop="cityChinese">
                    <el-input v-model="formData.cityChinese" placeholder="请输入城市(中文)" />
                </el-form-item>
                <el-form-item label="城市(英文)" prop="cityEnglish">
                    <el-input v-model="formData.cityEnglish" placeholder="请输入城市(英文)" />
                </el-form-item>
                <el-form-item label="街道地址(中文)" prop="streetChinese">
                    <el-input v-model="formData.streetChinese" placeholder="请输入街道地址(中文)" />
                </el-form-item>
                <el-form-item label="街道地址(英文)" prop="streetEnglish">
                    <el-input v-model="formData.streetEnglish" placeholder="请输入街道地址(英文)" />
                </el-form-item>
                <el-form-item label="部门/楼层(中文)" prop="departmentChinese">
                    <el-input
                        v-model="formData.departmentChinese"
                        placeholder="请输入部门/楼层(中文)"
                    />
                </el-form-item>
                <el-form-item label="部门/楼层(英文)" prop="departmentEnglish">
                    <el-input
                        v-model="formData.departmentEnglish"
                        placeholder="请输入部门/楼层(英文)"
                    />
                </el-form-item>
                <el-form-item label="邮件" prop="email">
                    <el-input v-model="formData.email" placeholder="请输入邮件" />
                </el-form-item>
                <el-form-item label="邮编" prop="postcode">
                    <el-input v-model="formData.postcode" placeholder="请输入邮编" />
                </el-form-item>
                <el-form-item label="手机" prop="phone">
                    <el-input v-model="formData.phone" placeholder="请输入手机" />
                </el-form-item>
                <el-form-item label="证件类型" prop="certificateType">
                    <el-select
                        class="flex-1"
                        v-model="formData.certificateType"
                        placeholder="请选择证件类型"
                    >
                        <el-option label="身份证" value="" />
                    </el-select>
                </el-form-item>
                <el-form-item label="证件号码" prop="idNumber">
                    <el-input v-model="formData.idNumber" placeholder="请输入证件号码" />
                </el-form-item>
                <el-form-item label="是否为技术联系人" prop="isTechnicist">
                    <el-select v-model="formData.isTechnicist" clearable>
                        <el-option label="是" value="1" />
                        <el-option label="否" value="2" />
                    </el-select>
                </el-form-item>

                <el-form-item label="座机" prop="landlinePhone">
                    <el-input v-model="formData.landlinePhone" placeholder="请输入座机" />
                </el-form-item>
                <el-form-item label="QQ" prop="qq">
                    <el-input v-model="formData.qq" placeholder="请输入QQ" />
                </el-form-item>
                <el-form-item label="创建人编号" prop="creatorId">
                    <el-input v-model="formData.creatorId" placeholder="请输入创建人编号" />
                </el-form-item>
                <el-form-item label="创建人名称" prop="creatorName">
                    <el-input v-model="formData.creatorName" placeholder="请输入创建人名称" />
                </el-form-item>

                <el-form-item label="更新人员编号" prop="updateStaffId">
                    <el-input v-model="formData.updateStaffId" placeholder="请输入更新人员编号" />
                </el-form-item>
                <el-form-item label="更新人员" prop="updateStaff">
                    <el-input v-model="formData.updateStaff" placeholder="请输入更新人员" />
                </el-form-item>
                <el-form-item label="更新日期" prop="contentUpdateTime">
                    <el-input v-model="formData.contentUpdateTime" placeholder="请输入更新日期" />
                </el-form-item>
                <el-form-item label="创建时间" prop="creatorTime">
                    <el-date-picker
                        class="flex-1 !flex"
                        v-model="formData.creatorTime"
                        type="datetime"
                        clearable
                        value-format="YYYY-MM-DD hh:mm:ss"
                        placeholder="请选择创建时间"
                    />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="formData.remark" placeholder="请输入备注" type="textarea" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import { inventorEdit, inventorAdd, inventorDetail } from '@/api/inventor'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '发明人详情' : '新增发明人列'
})

const formData = reactive({
    id: '',
    customerNameId: '',
    customerName: '',
    chineseName: '',
    englishName: '',
    jobNumber: '',
    surnameChinese: '',
    nameChinese: '',
    surnameEnglish: '',
    nameEnglish: '',
    nationality: '',
    isTechnicist: '',
    nation: '',
    province: '',
    cityChinese: '',
    cityEnglish: '',
    streetChinese: '',
    streetEnglish: '',
    departmentChinese: '',
    departmentEnglish: '',
    email: '',
    phone: '',
    certificateType: '',
    idNumber: '',
    postcode: '',
    landlinePhone: '',
    qq: '',
    creatorId: '',
    creatorName: '',
    creatorTime: '',
    updateStaffId: '',
    updateStaff: '',
    contentUpdateTime: '',
    remark: ''
})

const formRules = {
    chineseName: [
        {
            required: true,
            message: '请输入中文名',
            trigger: ['blur']
        }
    ]
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await inventorEdit(data) : await inventorAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await inventorDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
